#region Summary

// 
// Owner : JackieHan 
// 
// Solution : ResearchTest 
// 
// Project : JackieHan.ConfOrm.Test
// 
// File : TestDomain.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 测试为Domain类生成Nhibernate配置
// 
// History : 
//			2011-06-28 16:17:29	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using ConfOrm;
using ConfOrm.NH;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;

#endregion

namespace JackieHan.ConfOrm.Test
{
    ///<summary>
    ///测试为Domain类生成Nhibernate配置
    ///</summary>
    [TestFixture]
    public class TestDomain
    {
        [Test]
        public void JustForConf()
        {
            var hbmElement = GetMapping();
            Console.WriteLine(NhConfig.Serializer(hbmElement));
        }

        public HbmMapping GetMapping()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Domain>();
            var mapper = new Mapper(orm);
            return mapper.CompileMappingFor(new[] { typeof(Domain) });
        }

        [Test]
        public void Test()
        {
            var conf = NhConfig.ConfigureNhiberate();
            conf.AddDeserializedMapping(GetMapping(), "Domain");
            SchemaMetadataUpdater.QuoteTableAndColumns(conf);

            new SchemaExport(conf).Create(false, true);

            var factory = conf.BuildSessionFactory();

            using (var s = factory.OpenSession())
            {
                using (var tx = s.BeginTransaction())
                {
                    var domian = new Domain { Name = "JackieHan" };
                    s.Save(domian);
                    tx.Commit();
                }
            }

            using (var s = factory.OpenSession())
            {
                using (var tx = s.BeginTransaction())
                {
                    s.CreateQuery("delete from Domain").ExecuteUpdate();
                    tx.Commit();
                }
            }

            //new SchemaExport(conf).Drop(false, true);

        }
    }
}